/*
 * 文件名：UserService.java
 * 版权： Copyright 2002-2007 12316yun. All Rights Reserved.
 * 描述：〈描述〉
 
 * 修改人：王钰玺
 * 修改时间：2018/4/26 14:12
 * 修改单号：〈修改单号〉
 * 修改内容：〈修改内容〉
 */
package com.yun12316.project.system.demopart.service;

import com.yun12316.common.exception.DaoException;
import com.yun12316.project.system.demopart.dao.UserMapper;
import com.yun12316.project.system.demopart.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 纯单表 添删改查无需事务，涉及到同一方法内调用多张表的添删改必须有事务
 * <p>
 * <p>detailed comment </p>
 *
 * @author 王钰玺 2018/4/26
 * @see
 * @since 1.0
 */
@Service
@Transactional(readOnly = true,rollbackFor = DaoException.class)
public class UserService implements IUserService {

    @Resource
    private UserMapper userMapper;



    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据id删除一个对象
     * @param id
     * @throws DaoException
     */
    @Override
    @Transactional(rollbackFor = DaoException.class)
    public void deleteByPrimaryKey(Integer id) throws DaoException {
        try {
            userMapper.deleteByPrimaryKey(id);
        }catch (Exception ex) {
            throw new DaoException(ex.getMessage());
        }
    }

    /**
     * 保存一个对象
     * @param user
     */
    @Override
    @Transactional(rollbackFor = DaoException.class)
    public void save(User user) throws DaoException {
        try {
            if (user.getId() != null) {
                userMapper.updateByPrimaryKey(user);
            } else {
                userMapper.insert(user);
            }
        }catch (Exception ex) {
            throw new DaoException(ex.getMessage());
        }

    }

}
